module.exports = {
    // 添加文章
    add_article: async (ctx, next) => {
        const { title, content, coverImg, categoryId,user } = ctx.request.body
        let newImg = await ctx.service.file.save({dir:'/temporary'},{paths:coverImg,dir:'/images'})        
        try {
            let [res] = await ctx.db.query('insert into article(title,content,coverImg,categoryId,createTime,user) values(?,?,?,?,?,?)', [title, content, newImg, categoryId, new Date(),user])
            console.log(res);
            if(res.affectedRows > 0){
                ctx.body = {
                    code: 200,
                    msg: '发布成功'
                }
            }
        } catch (error) {
            ctx.body = {
                code: 201,
                msg: '创建失败' + error
            }
        }
    },
    // 修改文章
    update_article: async (ctx, next) => {
        try {
            const {id,title, content, coverImg, categoryId} = ctx.request.body
           let [res] = await ctx.db.query('update article set title=?,content=? ,coverImg=? ,categoryId=? ,updateTime=? where id=?',[title, content, coverImg, categoryId,new Date(),id])
            if(res.affectedRows > 0){
                ctx.body = {
                    code: 200,
                    msg: '更新成功'
                }
            }
        } catch (error) {
            ctx.body = {
                code: 201,
                msg: '更新失败' + error
            }
        }
    },
    // 删除文章
    del_article: async (ctx, next) => {
        const { id } = ctx.request.body
        try {
           let [res] = await ctx.db.query('update article set isDel=1 where id=?',[id])
            if(res.affectedRows > 0){
                ctx.body = {
                    code: 200,
                    msg: '删除成功'
                }
            }
        } catch (error) {
            ctx.body = {
                code: 201,
                msg: '删除失败' + error
            }
        }
    },
    // 分页获取文章列表
    get_article: async (ctx, next) => {
        const {startPage,endPage} =  await ctx.service.utils.paging(ctx)
        await ctx.db.query('select * from article where isdel=0 ').then(async res=>{
            const total =  res[0].length
            await ctx.db.query('select * from article where isdel=0  order by createTime desc limit ?,?',[startPage,endPage]).then(ress=>{
                ctx.body = {
                    code:200,
                    msg:'获取文章列表成功',
                    article:ress[0],
                    total:total
                }
            })
        })
    },
    // 获取文章详情
    detail_article:async (ctx, next) => {
        const {id} =  ctx.request.body
       try {
            let [res] =  await ctx.db.query('select * from article where id=? ',[id])
            ctx.body = {
                code: 200,
                msg: '获取详情成功',
                article:res[0]
            }
       } catch (error) {
        ctx.body = {
            code: 201,
            msg: '获取文章失败' + error
        }
       }
    },
}